package cn.wetcland.mimall;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import cn.wetcland.mimall.fragment.CartFragment;
import cn.wetcland.mimall.fragment.CategoryFragment;
import cn.wetcland.mimall.fragment.HomeFragment;
import cn.wetcland.mimall.fragment.StarFragment;
import cn.wetcland.mimall.fragment.UserFragment;

public class MainActivity extends AppCompatActivity {

    private FrameLayout mainContent;
    private ImageView tabUserIcon, tabCartIcon, tabStarIcon, tabHomeIcon, tabCategoryIcon;
    private TextView tabUserName, tabCartName, tabStarName, tabHomeName, tabCategoryName;
    private Fragment userFragment, starFragment, homeFragment, categoryFragment, cartFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        tabHomeClick(super.getCurrentFocus());
    }

    public void init() {
        mainContent = findViewById(R.id.main_content);
        tabUserIcon = findViewById(R.id.tab_user_icon);
        tabCartIcon = findViewById(R.id.tab_cart_icon);
        tabStarIcon = findViewById(R.id.tab_star_icon);
        tabHomeIcon = findViewById(R.id.tab_home_icon);
        tabCategoryIcon = findViewById(R.id.tab_category_icon);

        tabUserName = findViewById(R.id.tab_user_name);
        tabCartName = findViewById(R.id.tab_cart_name);
        tabStarName = findViewById(R.id.tab_star_name);
        tabHomeName = findViewById(R.id.tab_home_name);
        tabCategoryName = findViewById(R.id.tab_category_name);

        userFragment = new UserFragment();
        starFragment = new StarFragment();
        homeFragment = new HomeFragment();
        categoryFragment = new CategoryFragment();
        cartFragment = new CartFragment();
    }

    public void tabUserClick(View view) {
        clearTabSelected();
        tabUserIcon.setImageResource(R.drawable.icon_user_selected);
        tabUserName.setTextColor(Color.parseColor("#ff6700"));
        getSupportFragmentManager().beginTransaction().add(R.id.main_content, userFragment).commit();
    }

    public void tabCartClick(View view) {
        clearTabSelected();
        tabCartIcon.setImageResource(R.drawable.icon_cart_selected);
        tabCartName.setTextColor(Color.parseColor("#ff6700"));
        getSupportFragmentManager().beginTransaction().add(R.id.main_content, cartFragment).commit();
    }

    public void tabStarClick(View view) {
        clearTabSelected();
        tabStarIcon.setImageResource(R.drawable.icon_star_selected);
        tabStarName.setTextColor(Color.parseColor("#ff6700"));
        getSupportFragmentManager().beginTransaction().add(R.id.main_content, starFragment).commit();
    }

    public void tabCategoryClick(View view) {
        clearTabSelected();
        tabCategoryIcon.setImageResource(R.drawable.icon_category_selected);
        tabCategoryName.setTextColor(Color.parseColor("#ff6700"));
        getSupportFragmentManager().beginTransaction().add(R.id.main_content, categoryFragment).commit();
    }

    public void tabHomeClick(View view) {
        clearTabSelected();
        tabHomeIcon.setImageResource(R.drawable.icon_home_selected);
        tabHomeName.setTextColor(Color.parseColor("#ff6700"));
        getSupportFragmentManager().beginTransaction().add(R.id.main_content, homeFragment).commit();
    }

    public void clearTabSelected() {
        getSupportFragmentManager().beginTransaction()
                .remove(userFragment)
                .remove(starFragment)
                .remove(homeFragment)
                .remove(categoryFragment)
                .remove(cartFragment)
                .commit();
        tabHomeIcon.setImageResource(R.drawable.icon_home);
        tabHomeName.setTextColor(Color.parseColor("#000000"));
        tabCategoryIcon.setImageResource(R.drawable.icon_category);
        tabCategoryName.setTextColor(Color.parseColor("#000000"));
        tabStarIcon.setImageResource(R.drawable.icon_star);
        tabStarName.setTextColor(Color.parseColor("#000000"));
        tabUserIcon.setImageResource(R.drawable.icon_user);
        tabUserName.setTextColor(Color.parseColor("#000000"));
        tabCartIcon.setImageResource(R.drawable.icon_cart);
        tabCartName.setTextColor(Color.parseColor("#000000"));
    }
}